package com.jiawa.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.train.business.domain.DailyTrain;
import com.jiawa.train.business.domain.DailyTrainExample;
import com.jiawa.train.business.domain.Req.DailyTrainQueryReq;
import com.jiawa.train.business.domain.Req.DailyTrainSaveReq;
import com.jiawa.train.business.domain.Train;
import com.jiawa.train.business.domain.TrainExample;
import com.jiawa.train.business.domain.resp.DailyTrainQueryResp;
import com.jiawa.train.business.mapper.DailyTrainMapper;
import com.jiawa.train.business.mapper.TrainMapper;
import com.jiawa.train.common.resp.PageResp;
import com.jiawa.train.common.util.SnowUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class DailyTrainService {
    @Autowired
    private DailyTrainMapper dailyTrainMapper;
    @Autowired
    private TrainMapper trainMapper;
    @Autowired
    private DailyTrainCarriageService dailyTrainCarriageService;
    @Autowired
    private DailyTrainSeatService dailyTrainSeatService;
    @Autowired
    private DailyTrainTicketService dailyTrainTicketService;

    @Autowired
    private DailyTrainStationService dailyTrainStationService;
    public void save(DailyTrainSaveReq dailyTrainSaveReq) {
        DailyTrain dailyTrain = new DailyTrain();
        BeanUtils.copyProperties(dailyTrainSaveReq, dailyTrain);
        if(ObjectUtil.isNull(dailyTrain.getId())){
            dailyTrain.setId(SnowUtil.getSnowflakeNextId());
            dailyTrain.setCreateTime(new Date());
            dailyTrain.setUpdateTime(new Date());
            dailyTrainMapper.insert(dailyTrain);
        }else{
            dailyTrain.setUpdateTime(new Date());
            dailyTrainMapper.updateByPrimaryKeySelective(dailyTrain);
        }

    }

    public PageResp<DailyTrainQueryResp> queryList(DailyTrainQueryReq dailyTrainQueryReq) {
        DailyTrainExample example = new DailyTrainExample();
        example.setOrderByClause("date desc,code asc");
        PageHelper.startPage(dailyTrainQueryReq.getPage(),dailyTrainQueryReq.getSize());
        List<DailyTrain> dailyTrainList = dailyTrainMapper.selectByExample(example);
        PageInfo<DailyTrain> pageInfo = new PageInfo<>(dailyTrainList);

        List<DailyTrainQueryResp> respList = BeanUtil.copyToList(dailyTrainList, DailyTrainQueryResp.class);

        PageResp<DailyTrainQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(respList);
        return pageResp;
    }

    public void delete(Long id) {
        dailyTrainMapper.deleteByPrimaryKey(id);
    }

    /**
     * 生成默认所有车次信息，包括车次 车站 车厢 座位
     * @param date
     */
    public void getDaily(Date date) {
        TrainExample example = new TrainExample();
        List<Train> trainList = trainMapper.selectByExample(example);

        if(ObjectUtil.isEmpty(trainList)){
            return;
        }

        for(Train train : trainList){
            //删除该车次已有数据
            genDailyTrain(date,train);

        }
    }

    @Transactional
    public void genDailyTrain(Date date,Train train) {
        //删除该车次已有数据
        DailyTrainExample dailyTrainExample = new DailyTrainExample();
        dailyTrainExample.createCriteria()
                .andDateEqualTo(date)
                .andCodeEqualTo(train.getCode());
        dailyTrainMapper.deleteByExample(dailyTrainExample);

        //生成该车次数据
        DailyTrain dailyTrain = BeanUtil.copyProperties(train, DailyTrain.class);
        dailyTrain.setId(SnowUtil.getSnowflakeNextId());
        dailyTrain.setUpdateTime(new Date());
        dailyTrain.setCreateTime(new Date());
        dailyTrain.setDate(date);
        dailyTrainMapper.insert(dailyTrain);

        dailyTrainStationService.genDaily(date,train.getCode());
        dailyTrainCarriageService.genDaily(date,train.getCode());
        dailyTrainSeatService.genDaily(date,train.getCode());
        dailyTrainTicketService.genDaily(dailyTrain,date,train.getCode());

    }



}
